import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { CreateUserDto } from './dto/create-user.dto';
import { UpdateUserDto } from './dto/update-user.dto';
import { Repository } from 'typeorm';
import { User } from './entities/user.entity';
import { InjectRepository } from '@nestjs/typeorm';

@Injectable()
export class UserService {
  constructor(
    @InjectRepository(User)
    private readonly userRep: Repository<User>
  ) { }
  create(createUserDto: CreateUserDto) {
    return this.userRep.save(createUserDto);
  }

  findAll() {
    return `This action returns all user`;
  }

  async findOne(username: string) {
    const existUser = await this.userRep.findOne({
      where: { username: username }
    });
    if (existUser) throw new HttpException('用户不存在', HttpStatus.NOT_FOUND);
    return existUser;
  }

  update(id: number, updateUserDto: UpdateUserDto) {
    return `This action updates a #${id} user`;
  }

  remove(id: number) {
    return `This action removes a #${id} user`;
  }
}
